package com.yuanli.cosmeticmirror.utils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.webkit.JavascriptInterface;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yuanli.cosmeticmirror.base.Constant;
import com.yuanli.cosmeticmirror.bean.RingBean;

import java.lang.reflect.Type;

/**
 * 注入到js的类
 */
public class WebViewJsInject {
    private Handler handler;

    public WebViewJsInject(Handler handler) {
        this.handler = handler;
    }

    /**
     * h5⻚页⾯面切换的时候控制展示不不同的导航栏标题
     *
     * @param params
     */
    @JavascriptInterface
    public void changePage(String params) {
        // todo 客户端的逻辑代码
        Type type = new TypeToken<RingBean>() {}.getType();
        RingBean ringBean = new Gson().fromJson(params, type);

        Message msg = new Message();
        Bundle bundle = new Bundle();
        bundle.putParcelable("ringBean", ringBean);
        msg.setData(bundle);
        msg.what = Constant.TITLE;
        handler.sendMessage(msg);
    }

    /**
     * 在h5⻚页⾯面点击某个按钮后通知客户端关闭整个h5⻚页⾯面
     */
    @JavascriptInterface
    public void closeWindow() {
        // todo 客户端的逻辑代码
    }

    /**
     * 设置本地铃声
     * wno:铃音编号
     * wurl:铃音url
     * wname：铃音名称
     */
    @JavascriptInterface
    public void setRing(String wno, String wurl, String wname) {
        // todo 客户端的逻辑代码
        Message msg = new Message();
        Bundle bundle = new Bundle();
        bundle.putParcelable("ringBean", new RingBean(wno, wurl, wname));
        msg.setData(bundle);
        msg.what = Constant.RING;
        handler.sendMessage(msg);
    }

}
